import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:image_gallery_saver_plus/image_gallery_saver_plus.dart';
import 'package:lcapp/basic/user_info.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/model/toast/toast.dart';
import 'package:lcapp/page/home/home_share_page.dart';
import 'package:lcapp/widget/asr_cache_image.dart';
import 'package:lcapp/widget/lc_gesture_detector.dart';
import 'package:screenshot/screenshot.dart';
import 'package:path_provider/path_provider.dart';

class HomeShareImgPage extends StatefulWidget {
  final String? img;
  const HomeShareImgPage({Key? key, this.img}) : super(key: key);

  @override
  _HomeShareImgPageState createState() => _HomeShareImgPageState();
}

class _HomeShareImgPageState extends State<HomeShareImgPage> {
  ScreenshotController _screenshotController = ScreenshotController();
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.symmetric(vertical: 30.w),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Screenshot(
              controller: _screenshotController,
              child: Column(
                children: [
                  ASRCacheImage(
                    image: widget.img!,
                    width: 1.sw-60.w,
                  ),
                  SizedBox(height: 40.w,),
                  Container(
                    margin: EdgeInsets.symmetric(horizontal: 30.w),
                    color: Colors.white,
                    child: Row(
                      children: [
                        // QrImage(
                        //   size: 150.w,
                        //   padding: EdgeInsets.zero,
                        //   data: '${BasVueurl}/#/?lcID=${userInfo.id}',
                        // ),
                        SizedBox(width: 20.w,),
                        Text('打开粮策或微信扫一扫关注我',style: TextStyle(
                            color: Colors.black,
                            fontSize: 26.sp,
                            fontWeight: FontWeight.w600
                        ),),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 50.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  GestureDetector(
                    onTap: ()async{
                      final dir = await getTemporaryDirectory();
                      String path = dir.path;
                      String fileName =
                          DateTime.now().microsecondsSinceEpoch.toString() + '.jpg';
                      await _screenshotController.captureAndSave(
                          path, //set path where screenshot will be saved
                          fileName: fileName,
                          pixelRatio: window.devicePixelRatio ?? 2)
                          .then((value) {
                        ImageGallerySaverPlus.saveFile('$path/$fileName').then((value) {
                          showToast('保存成功~');
                          SmartDialog.dismiss();
                        });
                      });
                    },
                    child: Image.asset('assets/min/min_save_bg.png',width: 320.w,),
                  ),
                  SizedBox(width: 30.w,),
                  lcGestureDetector(
                    isLogin:true,
                    onTap: (){
                      SmartDialog.dismiss();
                      SmartDialog.show(
                        alignment: Alignment.bottomCenter,
                        clickMaskDismiss: true,
                        maskColor: Color.fromRGBO(0, 0, 0, 0.5), builder: (BuildContext context) {
                          return HomeSharepage();
                      },
                      );
                    },
                    child: Image.asset('assets/min/min_share_bg.png',width: 320.w,),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
